<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Admin;
use App\Model\StudentClass;
use App\Model\Feedback;
use App\Model\FeedbackTeacher;
use DB;

/**
 * 学生反馈
 */
class FeedbackController extends Controller
{

    /**
     * 反馈意见view
     *
     * @return view
     */
    public function feedbackView()
    {
        $classes = StudentClass::select('id', 'class_type', 'class_name')->where('class_start_time', '<', millisecond())->limit(12)->get();
        $classGroup = $classes->groupBy('class_type');
        $teachers = Admin::select('id', 'role_id', 'name')->where('status', 0)->whereIn('role_id', [3, 4])->get();
        $teacherGroup = $teachers->groupBy('role_id');
        $assign = [
            'class_ui' => $classGroup[1] ?? [],
            'class_h5' => $classGroup[2] ?? [],
            'teacher_ui' => $teacherGroup[3] ?? [],
            'teacher_h5' => $teacherGroup[4] ?? [],
        ];
        return view('feedback', $assign);
    }

    /**
     * 提交反馈意见
     *
     * @param Request $request
     * @return json
     */
    public function feedback(Request $request)
    {
        $fingerprint = $request->fingerprint;
        $fingerCheck = Feedback::where('fingerprint', $fingerprint)->first(); // 检测此用户此班级是否评价过
        if ($fingerCheck)  return resJson(self::CUSTOM_10004_FINGERPRINT_ERROR);
        DB::transaction(function () use ($request) {
            $inputs = $request->all();
            $time = millisecond();
            $feedback = [
                'ip' => $request->ip(),
                'class_id' => $inputs['class_id'],
                'class_name' => StudentClass::where('id', $inputs['class_id'])->value('class_name'),
                'course_star' => $inputs['course_star'],
                'course_advice' => $inputs['course_advice'],
                'zx_advice' => $inputs['zx_advice'],
                'hope' => $inputs['hope'],
                'fingerprint' => $inputs['fingerprint'],
                'created_time' => time()
            ];
            $feedback = Feedback::create($feedback);
            $feedback_id = $feedback->id;
            $teachers = collect($inputs['teachers'])->map(function ($item) use ($feedback_id, $time, $inputs) {
                $item['class_id'] = $inputs['class_id'];
                $item['feedback_id'] = $feedback_id;
                $item['created_time'] = $time;
                return $item;
            })->all();
            FeedbackTeacher::insert($teachers);
        }, 5);
        return resJson(self::HTTP_0_SUCCESS);
    }

    public function index(Request $request)
    {
        $class_id = $request->class_id;
        $feedback = Feedback::where('class_id', $class_id)->get();
        $total_avg = round($feedback->avg('course_star'), 2); // 课程平均分 
        $feedbackTeacher = FeedbackTeacher::where('class_id', $class_id)->get();
        $teachers = $feedbackTeacher->groupBy('teacher_name')
            ->map(function ($item, $key) {
                return [
                    'name' => $key,
                    'star' => round($item->avg('teacher_star'), 2)
                ];
            })
            ->values()
            ->all();
        return resJson(self::HTTP_0_SUCCESS, [
            'total_avg' => $total_avg,
            'teachers' => $teachers,
            'list' => $feedback->pluck('id')
        ]);
    }

    public function info(Request $request, $id)
    {
        $data = Feedback::find($id);
        $res = $data->toArray();
        $res['teachers'] = $data->teachers;
        return resJson(self::HTTP_0_SUCCESS, $res);
    }
}
